<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use think\Request;

class Kuaishou extends Controller
{
    public function index($date='', $sex='')
    {
        $date = $date ?: date('Ymd');
        $dirs = $sex ? [$sex] : ['female', 'male'];
    
        foreach ($dirs as $dir) {
            $path = "/disk/uploads/videos/kuaishou/";

            $folder = "." . $path . "$date/$dir/video";

            $files = $this->scanFile($folder, 'mp4');

            if ($files) {
                $kefu = Db::name('config_system')->where(['title' => 'id'])->value('content');
                $kefu = (int)strip_tags($kefu);

                $where = ['role' => 1];

                if ($kefu) {
                    $where['id'] = ['neq', $kefu];
                }
                switch ($dir) {
                    case 'male':
                        $where['sex'] = '男';
                        break;
                    case 'female':
                        $where['sex'] = '女';
                        break;
                    default:
                        break;
                }

                $users = Db::name('user')->where($where)->field('id, name, city')->select();

                if ($users) {
                    $count = count($users);
                    $current_time = time();
                    $start_today = strtotime(date('Ymd'));

                    $videos = Db::name('dymanic')->where(['video' => ['like', [$path . '%']]])->column('content, video');

                    $video_videos = array_column($videos, 'video');
                    $video_contents = array_column($videos, 'content');

                    $contain = [];
                    foreach ($files as $item) {
                        if (!in_array($item, $video_videos)) {
                            $name = substr($item, 0, strrpos($item, '.mp4'));

                            if (!in_array($item, $video_contents)) {
                                $rand_date = date('Y-m-d H:i:s', mt_rand($start_today, time()));

                                $rename = 'file' . time() . uniqid();

                                $old_video_file = $path . "$date/$dir/video/$item";
                                $old_cover_file = $path . "$date/$dir/cover/$name.png";
                                $new_video_file = $path . "$date/$dir/video/$rename.mp4";
                                $new_cover_file = $path . "$date/$dir/cover/$rename.png";

                                rename(ROOT_PATH . trim($old_video_file, '/'), ROOT_PATH . trim($new_video_file, '/'));
                                rename(ROOT_PATH . trim($old_cover_file, '/'), ROOT_PATH . trim($new_cover_file, '/'));

                                $rand_user = $users[mt_rand(0, $count-1)];

                                $contain[] = [
                                    'type' => 3,
                                    'userid' => $rand_user['id'],
                                    'content' => $name,
                                    'video' => $new_video_file,
                                    'cover' => $new_cover_file,
                                    'citys' => $rand_user['city'],
                                    'lover' => mt_rand(0,100),
                                    'thumbsup' => mt_rand(0,100),
                                    'visit' => mt_rand(0,100),
                                    'addtime' => $rand_date,
                                    'shenhe' => 1,
                                    'shenhe_time' => $current_time,
                                ];
                            }
                        }
                    }

                    if ($contain) {
                        Db::name('dymanic')->insertAll($contain);
                    }
                }
            }
        }
    }

    private function scanFile($path, $format='') {
        global $result;

        $files = scandir($path);
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                if ($format) {
                    if (substr($file, -4) === ".$format" && stripos($file, 'file') === false) {
                        $result[] = basename($file);
                    }
                } else {
                    if (stripos($file, 'file') === false) {
                        $result[] = basename($file);
                    }
                }
            }
        }
        return $result;
    }
}
